<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Data type objects (dtype) &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../about.html" >
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../index.html" >
    <link rel="up" title="Array objects" href="arrays.html" >
    <link rel="next" title="numpy.dtype" href="generated/numpy.dtype.html" >
    <link rel="prev" title="numpy.generic.__setstate__" href="generated/numpy.generic.__setstate__.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../index.html">
      <img border=0 alt="NumPy" src="../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="index.html" >NumPy Reference</a></li>
          <li class="active"><a href="arrays.html" accesskey="U">Array objects</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="generated/numpy.dtype.html" title="numpy.dtype"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="generated/numpy.generic.__setstate__.html" title="numpy.generic.__setstate__"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Data type objects (<code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code>)</a><ul>
<li><a class="reference internal" href="#specifying-and-constructing-data-types">Specifying and constructing data types</a></li>
<li><a class="reference internal" href="#dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a><ul>
<li><a class="reference internal" href="#attributes">Attributes</a><ul>
</ul>
</li>
<li><a class="reference internal" href="#methods">Methods</a><ul>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="generated/numpy.generic.__setstate__.html"
                        title="previous chapter">numpy.generic.__setstate__</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="generated/numpy.dtype.html"
                        title="next chapter">numpy.dtype</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="data-type-objects-dtype">
<span id="arrays-dtypes"></span><h1>Data type objects (<a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a>)<a class="headerlink" href="#data-type-objects-dtype" title="Permalink to this headline">¶</a></h1>
<p>A data type object (an instance of <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">numpy.dtype</span></code></a> class)
describes how the bytes in the fixed-size block of memory
corresponding to an array item should be interpreted. It describes the
following aspects of the data:</p>
<ol class="arabic simple">
<li><p>Type of the data (integer, float, Python object, etc.)</p></li>
<li><p>Size of the data (how many bytes is in <em>e.g.</em> the integer)</p></li>
<li><p>Byte order of the data (<a class="reference internal" href="../glossary.html#term-little-endian"><span class="xref std std-term">little-endian</span></a> or <a class="reference internal" href="../glossary.html#term-big-endian"><span class="xref std std-term">big-endian</span></a>)</p></li>
<li><p>If the data type is <a class="reference internal" href="../glossary.html#term-structured-data-type"><span class="xref std std-term">structured data type</span></a>, an aggregate of other
data types, (<em>e.g.</em>, describing an array item consisting of
an integer and a float),</p>
<ol class="arabic simple">
<li><p>what are the names of the “<a class="reference internal" href="../glossary.html#term-field"><span class="xref std std-term">fields</span></a>” of the structure,
by which they can be <a class="reference internal" href="arrays.indexing.html#arrays-indexing-fields"><span class="std std-ref">accessed</span></a>,</p></li>
<li><p>what is the data-type of each <a class="reference internal" href="../glossary.html#term-field"><span class="xref std std-term">field</span></a>, and</p></li>
<li><p>which part of the memory block each field takes.</p></li>
</ol>
</li>
<li><p>If the data type is a sub-array, what is its shape and data type.</p></li>
</ol>
<p id="index-0">To describe the type of scalar data, there are several <a class="reference internal" href="arrays.scalars.html#arrays-scalars-built-in"><span class="std std-ref">built-in
scalar types</span></a> in NumPy for various precision
of integers, floating-point numbers, <em>etc</em>. An item extracted from an
array, <em>e.g.</em>, by indexing, will be a Python object whose type is the
scalar type associated with the data type of the array.</p>
<p>Note that the scalar types are not <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a> objects, even though
they can be used in place of one whenever a data type specification is
needed in NumPy.</p>
<p id="index-1">Structured data types are formed by creating a data type whose
<a class="reference internal" href="../glossary.html#term-field"><span class="xref std std-term">field</span></a> contain other data types. Each field has a name by
which it can be <a class="reference internal" href="arrays.indexing.html#arrays-indexing-fields"><span class="std std-ref">accessed</span></a>. The parent data
type should be of sufficient size to contain all its fields; the
parent is nearly always based on the <code class="xref py py-class docutils literal notranslate"><span class="pre">void</span></code> type which allows
an arbitrary item size. Structured data types may also contain nested
structured sub-array data types in their fields.</p>
<p id="index-2">Finally, a data type can describe items that are themselves arrays of
items of another data type. These sub-arrays must, however, be of a
fixed size.</p>
<p>If an array is created using a data-type describing a sub-array,
the dimensions of the sub-array are appended to the shape
of the array when the array is created. Sub-arrays in a field of a
structured type behave differently, see <a class="reference internal" href="arrays.indexing.html#arrays-indexing-fields"><span class="std std-ref">Field Access</span></a>.</p>
<p>Sub-arrays always have a C-contiguous memory layout.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<p>A simple data type containing a 32-bit big-endian integer:
(see <a class="reference internal" href="#arrays-dtypes-constructing"><span class="std std-ref">Specifying and constructing data types</span></a> for details on construction)</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;&gt;i4&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">byteorder</span>
<span class="go">&#39;&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">itemsize</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;int32&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="o">.</span><span class="n">type</span> <span class="ow">is</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The corresponding array scalar type is <code class="xref py py-class docutils literal notranslate"><span class="pre">int32</span></code>.</p>
</div>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<p>A structured data type containing a 16-character string (in field ‘name’)
and a sub-array of two 64-bit floating-point number (in field ‘grades’):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">([(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">unicode_</span><span class="p">,</span> <span class="mi">16</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;grades&#39;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,))])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
<span class="go">dtype(&#39;|U16&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span><span class="p">[</span><span class="s1">&#39;grades&#39;</span><span class="p">]</span>
<span class="go">dtype((&#39;float64&#39;,(2,)))</span>
</pre></div>
</div>
<p>Items of an array of this data type are wrapped in an <a class="reference internal" href="arrays.scalars.html#arrays-scalars"><span class="std std-ref">array
scalar</span></a> type that also has two fields:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([(</span><span class="s1">&#39;Sarah&#39;</span><span class="p">,</span> <span class="p">(</span><span class="mf">8.0</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">)),</span> <span class="p">(</span><span class="s1">&#39;John&#39;</span><span class="p">,</span> <span class="p">(</span><span class="mf">6.0</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">))],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dt</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">(&#39;John&#39;, [6.0, 7.0])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="s1">&#39;grades&#39;</span><span class="p">]</span>
<span class="go">array([ 6.,  7.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="go">&lt;type &#39;numpy.void&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="s1">&#39;grades&#39;</span><span class="p">])</span>
<span class="go">&lt;type &#39;numpy.ndarray&#39;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="specifying-and-constructing-data-types">
<span id="arrays-dtypes-constructing"></span><h2>Specifying and constructing data types<a class="headerlink" href="#specifying-and-constructing-data-types" title="Permalink to this headline">¶</a></h2>
<p>Whenever a data-type is required in a NumPy function or method, either
a <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a> object or something that can be converted to one can
be supplied.  Such conversions are done by the <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a>
constructor:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype</span></code></a>(obj[, align, copy])</p></td>
<td><p>Create a data type object.</p></td>
</tr>
</tbody>
</table>
<p>What can be converted to a data-type object is described below:</p>
<p><a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a> object</p>
<blockquote>
<div><p id="index-3">Used as-is.</p>
</div></blockquote>
<p>None</p>
<blockquote>
<div><p id="index-4">The default data type: <code class="xref py py-class docutils literal notranslate"><span class="pre">float_</span></code>.</p>
</div></blockquote>
<p id="index-5">Array-scalar types</p>
<blockquote>
<div><p>The 24 built-in <a class="reference internal" href="arrays.scalars.html#arrays-scalars-built-in"><span class="std std-ref">array scalar type objects</span></a> all convert to an associated data-type object.
This is true for their sub-classes as well.</p>
<p>Note that not all data-type information can be supplied with a
type-object: for example, <code class="xref py py-obj docutils literal notranslate"><span class="pre">flexible</span></code> data-types have
a default <em>itemsize</em> of 0, and require an explicitly given size
to be useful.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>      <span class="c1"># 32-bit integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">complex128</span><span class="p">)</span> <span class="c1"># 128-bit complex floating-point number</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p>Generic types</p>
<blockquote>
<div><p>The generic hierarchical type objects convert to corresponding
type objects according to the associations:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 78%" />
<col style="width: 22%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">number</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">inexact</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">floating</span></code></p></td>
<td><p><a class="reference external" href="https://docs.python.org/dev/library/functions.html#float" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complexfloating</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">cfloat</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">integer</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">signedinteger</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int_</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">unsignedinteger</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uint</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">character</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.html#numpy.generic" title="numpy.generic"><code class="xref py py-class docutils literal notranslate"><span class="pre">generic</span></code></a>, <code class="xref py py-class docutils literal notranslate"><span class="pre">flexible</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">void</span></code></p></td>
</tr>
</tbody>
</table>
</div></blockquote>
<p>Built-in Python types</p>
<blockquote>
<div><p>Several python types are equivalent to a corresponding
array scalar when used to generate a <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a> object:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 22%" />
<col style="width: 78%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/dev/library/functions.html#int" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int_</span></code></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/dev/library/functions.html#bool" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bool_</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/dev/library/functions.html#float" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float_</span></code></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/dev/library/functions.html#complex" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">complex</span></code></a></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">cfloat</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/dev/library/stdtypes.html#bytes" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes_</span></code></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/dev/library/stdtypes.html#str" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes_</span></code> (Python2) or <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode_</span></code> (Python3)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode_</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">buffer</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">void</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>(all others)</p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">object_</span></code></p></td>
</tr>
</tbody>
</table>
<p>Note that <code class="docutils literal notranslate"><span class="pre">str</span></code> refers to either null terminated bytes or unicode strings
depending on the Python version. In code targeting both Python 2 and 3
<code class="docutils literal notranslate"><span class="pre">np.unicode_</span></code> should be used as a dtype for strings.
See <a class="reference internal" href="#string-dtype-note"><span class="std std-ref">Note on string types</span></a>.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="nb">float</span><span class="p">)</span>   <span class="c1"># Python-compatible floating-point number</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>     <span class="c1"># Python-compatible integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>  <span class="c1"># Python object</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p>Types with <code class="docutils literal notranslate"><span class="pre">.dtype</span></code></p>
<blockquote>
<div><p>Any type object with a <code class="docutils literal notranslate"><span class="pre">dtype</span></code> attribute: The attribute will be
accessed and used directly. The attribute must return something
that is convertible into a dtype object.</p>
</div></blockquote>
<p id="index-6">Several kinds of strings can be converted. Recognized strings can be
prepended with <code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code> (<a class="reference internal" href="../glossary.html#term-big-endian"><span class="xref std std-term">big-endian</span></a>), <code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code>
(<a class="reference internal" href="../glossary.html#term-little-endian"><span class="xref std std-term">little-endian</span></a>), or <code class="docutils literal notranslate"><span class="pre">'='</span></code> (hardware-native, the default), to
specify the byte order.</p>
<p>One-character strings</p>
<blockquote>
<div><p>Each built-in data-type has a character code
(the updated Numeric typecodes), that uniquely identifies it.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">)</span>  <span class="c1"># byte, native byte order</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;&gt;H&#39;</span><span class="p">)</span> <span class="c1"># big-endian unsigned short</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;&lt;f&#39;</span><span class="p">)</span> <span class="c1"># little-endian single-precision float</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">)</span>  <span class="c1"># double-precision floating-point number</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p>Array-protocol type strings (see <a class="reference internal" href="arrays.interface.html#arrays-interface"><span class="std std-ref">The Array Interface</span></a>)</p>
<blockquote>
<div><p>The first character specifies the kind of data and the remaining
characters specify the number of bytes per item, except for Unicode,
where it is interpreted as the number of characters.  The item size
must correspond to an existing type, or an error will be raised.  The
supported kinds are</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 29%" />
<col style="width: 71%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'?'</span></code></p></td>
<td><p>boolean</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
<td><p>(signed) byte</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>
<td><p>unsigned byte</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'i'</span></code></p></td>
<td><p>(signed) integer</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code></p></td>
<td><p>unsigned integer</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
<td><p>floating-point</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td>
<td><p>complex-floating point</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'m'</span></code></p></td>
<td><p>timedelta</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'M'</span></code></p></td>
<td><p>datetime</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'O'</span></code></p></td>
<td><p>(Python) objects</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'S'</span></code>, <code class="docutils literal notranslate"><span class="pre">'a'</span></code></p></td>
<td><p>zero-terminated bytes (not recommended)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'U'</span></code></p></td>
<td><p>Unicode string</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'V'</span></code></p></td>
<td><p>raw data (<code class="xref py py-class docutils literal notranslate"><span class="pre">void</span></code>)</p></td>
</tr>
</tbody>
</table>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;i4&#39;</span><span class="p">)</span>   <span class="c1"># 32-bit signed integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;f8&#39;</span><span class="p">)</span>   <span class="c1"># 64-bit floating-point number</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;c16&#39;</span><span class="p">)</span>  <span class="c1"># 128-bit complex floating-point number</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;a25&#39;</span><span class="p">)</span>  <span class="c1"># 25-length zero-terminated bytes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;U25&#39;</span><span class="p">)</span>  <span class="c1"># 25-character string</span>
</pre></div>
</div>
</div>
<div class="admonition-note-on-string-types admonition" id="string-dtype-note">
<p class="admonition-title">Note on string types</p>
<p>For backward compatibility with Python 2 the <code class="docutils literal notranslate"><span class="pre">S</span></code> and <code class="docutils literal notranslate"><span class="pre">a</span></code> typestrings
remain zero-terminated bytes and <code class="docutils literal notranslate"><span class="pre">np.string_</span></code> continues to map to
<code class="docutils literal notranslate"><span class="pre">np.bytes_</span></code>.
To use actual strings in Python 3 use <code class="docutils literal notranslate"><span class="pre">U</span></code> or <code class="docutils literal notranslate"><span class="pre">np.unicode_</span></code>.
For signed bytes that do not need zero-termination <code class="docutils literal notranslate"><span class="pre">b</span></code> or <code class="docutils literal notranslate"><span class="pre">i1</span></code> can be
used.</p>
</div>
</div></blockquote>
<p>String with comma-separated fields</p>
<blockquote>
<div><p>A short-hand notation for specifying the format of a structured data type is
a comma-separated string of basic formats.</p>
<p>A basic format in this context is an optional shape specifier
followed by an array-protocol type string. Parenthesis are required
on the shape if it has more than one dimension. NumPy allows a modification
on the format in that any string that can uniquely identify the
type can be used to specify the data-type in a field.
The generated data-type fields are named <code class="docutils literal notranslate"><span class="pre">'f0'</span></code>, <code class="docutils literal notranslate"><span class="pre">'f1'</span></code>, …,
<code class="docutils literal notranslate"><span class="pre">'f&lt;N-1&gt;'</span></code> where N (&gt;1) is the number of comma-separated basic
formats in the string. If the optional shape specifier is provided,
then the data-type for the corresponding field describes a sub-array.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<ul class="simple">
<li><p>field named <code class="docutils literal notranslate"><span class="pre">f0</span></code> containing a 32-bit integer</p></li>
<li><p>field named <code class="docutils literal notranslate"><span class="pre">f1</span></code> containing a 2 x 3 sub-array
of 64-bit floating-point numbers</p></li>
<li><p>field named <code class="docutils literal notranslate"><span class="pre">f2</span></code> containing a 32-bit floating-point number</p></li>
</ul>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s2">&quot;i4, (2,3)f8, f4&quot;</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>field named <code class="docutils literal notranslate"><span class="pre">f0</span></code> containing a 3-character string</p></li>
<li><p>field named <code class="docutils literal notranslate"><span class="pre">f1</span></code> containing a sub-array of shape (3,)
containing 64-bit unsigned integers</p></li>
<li><p>field named <code class="docutils literal notranslate"><span class="pre">f2</span></code> containing a 3 x 4 sub-array
containing 10-character strings</p></li>
</ul>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s2">&quot;a3, 3u8, (3,4)a10&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p>Type strings</p>
<blockquote>
<div><p>Any string in <code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.sctypeDict</span></code>.keys():</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;uint32&#39;</span><span class="p">)</span>   <span class="c1"># 32-bit unsigned integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;Float64&#39;</span><span class="p">)</span>  <span class="c1"># 64-bit floating-point number</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p id="index-7"><code class="docutils literal notranslate"><span class="pre">(flexible_dtype,</span> <span class="pre">itemsize)</span></code></p>
<blockquote>
<div><p>The first argument must be an object that is converted to a
zero-sized flexible data-type object, the second argument is
an integer providing the desired itemsize.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">void</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>  <span class="c1"># 10-byte wide data block</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="s1">&#39;U&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>   <span class="c1"># 10-character unicode string</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">(fixed_dtype,</span> <span class="pre">shape)</span></code></p>
<blockquote>
<div><p id="index-8">The first argument is any object that can be converted into a
fixed-size data-type object. The second argument is the desired
shape of this type. If the shape parameter is 1, then the
data-type object is equivalent to fixed dtype. If <em>shape</em> is a
tuple, then the new dtype defines a sub-array of the given shape.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>          <span class="c1"># 2 x 2 integer sub-array</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="s1">&#39;U10&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>                 <span class="c1"># 10-character string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="s1">&#39;i4, (2,3)f8, f4&#39;</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> <span class="c1"># 2 x 3 structured sub-array</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p id="index-9"><code class="docutils literal notranslate"><span class="pre">[(field_name,</span> <span class="pre">field_dtype,</span> <span class="pre">field_shape),</span> <span class="pre">...]</span></code></p>
<blockquote>
<div><p><em>obj</em> should be a list of fields where each field is described by a
tuple of length 2 or 3. (Equivalent to the <code class="docutils literal notranslate"><span class="pre">descr</span></code> item in the
<a class="reference internal" href="arrays.interface.html#__array_interface__" title="__array_interface__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">__array_interface__</span></code></a> attribute.)</p>
<p>The first element, <em>field_name</em>, is the field name (if this is
<code class="docutils literal notranslate"><span class="pre">''</span></code> then a standard field name, <code class="docutils literal notranslate"><span class="pre">'f#'</span></code>, is assigned).  The
field name may also be a 2-tuple of strings where the first string
is either a “title” (which may be any string or unicode string) or
meta-data for the field which can be any object, and the second
string is the “name” which must be a valid Python identifier.</p>
<p>The second element, <em>field_dtype</em>, can be anything that can be
interpreted as a data-type.</p>
<p>The optional third element <em>field_shape</em> contains the shape if this
field represents an array of the data-type in the second
element. Note that a 3-tuple with a third argument equal to 1 is
equivalent to a 2-tuple.</p>
<p>This style does not accept <em>align</em> in the <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a>
constructor as it is assumed that all of the memory is accounted
for by the array interface description.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<p>Data-type with fields <code class="docutils literal notranslate"><span class="pre">big</span></code> (big-endian 32-bit integer) and
<code class="docutils literal notranslate"><span class="pre">little</span></code> (little-endian 32-bit integer):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">([(</span><span class="s1">&#39;big&#39;</span><span class="p">,</span> <span class="s1">&#39;&gt;i4&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;little&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;i4&#39;</span><span class="p">)])</span>
</pre></div>
</div>
<p>Data-type with fields <code class="docutils literal notranslate"><span class="pre">R</span></code>, <code class="docutils literal notranslate"><span class="pre">G</span></code>, <code class="docutils literal notranslate"><span class="pre">B</span></code>, <code class="docutils literal notranslate"><span class="pre">A</span></code>, each being an
unsigned 8-bit integer:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">([(</span><span class="s1">&#39;R&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;G&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;B&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">)])</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p id="index-10"><code class="docutils literal notranslate"><span class="pre">{'names':</span> <span class="pre">...,</span> <span class="pre">'formats':</span> <span class="pre">...,</span> <span class="pre">'offsets':</span> <span class="pre">...,</span> <span class="pre">'titles':</span> <span class="pre">...,</span> <span class="pre">'itemsize':</span> <span class="pre">...}</span></code></p>
<blockquote>
<div><p>This style has two required and three optional keys.  The <em>names</em>
and <em>formats</em> keys are required. Their respective values are
equal-length lists with the field names and the field formats.
The field names must be strings and the field formats can be any
object accepted by <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a> constructor.</p>
<p>When the optional keys <em>offsets</em> and <em>titles</em> are provided,
their values must each be lists of the same length as the <em>names</em>
and <em>formats</em> lists. The <em>offsets</em> value is a list of byte offsets
(limited to <a class="reference external" href="https://docs.python.org/dev/library/ctypes.html#ctypes.c_int" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes.c_int</span></code></a>) for each field, while the <em>titles</em> value is a
list of titles for each field (None can be used if no title is
desired for that field). The <em>titles</em> can be any <code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code>
or <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code> object and will add another entry to the
fields dictionary keyed by the title and referencing the same
field tuple which will contain the title as an additional tuple
member.</p>
<p>The <em>itemsize</em> key allows the total size of the dtype to be
set, and must be an integer large enough so all the fields
are within the dtype. If the dtype being constructed is aligned,
the <em>itemsize</em> must also be divisible by the struct alignment. Total dtype
<em>itemsize</em> is limited to <a class="reference external" href="https://docs.python.org/dev/library/ctypes.html#ctypes.c_int" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes.c_int</span></code></a>.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<p>Data type with fields <code class="docutils literal notranslate"><span class="pre">r</span></code>, <code class="docutils literal notranslate"><span class="pre">g</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span></code>, <code class="docutils literal notranslate"><span class="pre">a</span></code>, each being
an 8-bit unsigned integer:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">({</span><span class="s1">&#39;names&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;r&#39;</span><span class="p">,</span><span class="s1">&#39;g&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">,</span><span class="s1">&#39;a&#39;</span><span class="p">],</span>
<span class="gp">... </span>               <span class="s1">&#39;formats&#39;</span><span class="p">:</span> <span class="p">[</span><span class="n">uint8</span><span class="p">,</span> <span class="n">uint8</span><span class="p">,</span> <span class="n">uint8</span><span class="p">,</span> <span class="n">uint8</span><span class="p">]})</span>
</pre></div>
</div>
<p>Data type with fields <code class="docutils literal notranslate"><span class="pre">r</span></code> and <code class="docutils literal notranslate"><span class="pre">b</span></code> (with the given titles),
both being 8-bit unsigned integers, the first at byte position
0 from the start of the field and the second at position 2:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">({</span><span class="s1">&#39;names&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;r&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">],</span> <span class="s1">&#39;formats&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;u1&#39;</span><span class="p">,</span> <span class="s1">&#39;u1&#39;</span><span class="p">],</span>
<span class="gp">... </span>               <span class="s1">&#39;offsets&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp">... </span>               <span class="s1">&#39;titles&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;Red pixel&#39;</span><span class="p">,</span> <span class="s1">&#39;Blue pixel&#39;</span><span class="p">]})</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">{'field1':</span> <span class="pre">...,</span> <span class="pre">'field2':</span> <span class="pre">...,</span> <span class="pre">...}</span></code></p>
<blockquote>
<div><p>This usage is discouraged, because it is ambiguous with the
other dict-based construction method. If you have a field
called ‘names’ and a field called ‘formats’ there will be
a conflict.</p>
<p>This style allows passing in the <a class="reference internal" href="generated/numpy.dtype.fields.html#numpy.dtype.fields" title="numpy.dtype.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a>
attribute of a data-type object.</p>
<p><em>obj</em> should contain string or unicode keys that refer to
<code class="docutils literal notranslate"><span class="pre">(data-type,</span> <span class="pre">offset)</span></code> or <code class="docutils literal notranslate"><span class="pre">(data-type,</span> <span class="pre">offset,</span> <span class="pre">title)</span></code> tuples.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<p>Data type containing field <code class="docutils literal notranslate"><span class="pre">col1</span></code> (10-character string at
byte position 0), <code class="docutils literal notranslate"><span class="pre">col2</span></code> (32-bit float at byte position 10),
and <code class="docutils literal notranslate"><span class="pre">col3</span></code> (integers at byte position 14):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">({</span><span class="s1">&#39;col1&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;U10&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="s1">&#39;col2&#39;</span><span class="p">:</span> <span class="p">(</span><span class="n">float32</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="go">    &#39;col3&#39;: (int, 14)})</span>
</pre></div>
</div>
</div>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">(base_dtype,</span> <span class="pre">new_dtype)</span></code></p>
<blockquote>
<div><p>In NumPy 1.7 and later, this form allows <em class="xref py py-obj">base_dtype</em> to be interpreted as
a structured dtype. Arrays created with this dtype will have underlying
dtype <em class="xref py py-obj">base_dtype</em> but will have fields and flags taken from <em class="xref py py-obj">new_dtype</em>.
This is useful for creating custom structured dtypes, as done in
<a class="reference internal" href="arrays.classes.html#arrays-classes-rec"><span class="std std-ref">record arrays</span></a>.</p>
<p>This form also makes it possible to specify struct dtypes with overlapping
fields, functioning like the ‘union’ type in C. This usage is discouraged,
however, and the union mechanism is preferred.</p>
<p>Both arguments must be convertible to data-type objects with the same total
size.</p>
<div class="admonition-example admonition">
<p class="admonition-title">Example</p>
<p>32-bit integer, whose first two bytes are interpreted as an integer
via field <code class="docutils literal notranslate"><span class="pre">real</span></code>, and the following two bytes via field <code class="docutils literal notranslate"><span class="pre">imag</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">,{</span><span class="s1">&#39;real&#39;</span><span class="p">:(</span><span class="n">np</span><span class="o">.</span><span class="n">int16</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span><span class="s1">&#39;imag&#39;</span><span class="p">:(</span><span class="n">np</span><span class="o">.</span><span class="n">int16</span><span class="p">,</span> <span class="mi">2</span><span class="p">)})</span>
</pre></div>
</div>
<p>32-bit integer, which is interpreted as consisting of a sub-array
of shape <code class="docutils literal notranslate"><span class="pre">(4,)</span></code> containing 8-bit integers:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int8</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span>
</pre></div>
</div>
<p>32-bit integer, containing fields <code class="docutils literal notranslate"><span class="pre">r</span></code>, <code class="docutils literal notranslate"><span class="pre">g</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span></code>, <code class="docutils literal notranslate"><span class="pre">a</span></code> that
interpret the 4 bytes in the integer as four unsigned integers:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">((</span><span class="s1">&#39;i4&#39;</span><span class="p">,</span> <span class="p">[(</span><span class="s1">&#39;r&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">),(</span><span class="s1">&#39;g&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">),(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">),(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;u1&#39;</span><span class="p">)]))</span>
</pre></div>
</div>
</div>
</div></blockquote>
</div>
<div class="section" id="dtype">
<h2><a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a><a class="headerlink" href="#dtype" title="Permalink to this headline">¶</a></h2>
<p>NumPy data type descriptions are instances of the <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a> class.</p>
<div class="section" id="attributes">
<h3>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h3>
<p>The type of the data is described by the following <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-class docutils literal notranslate"><span class="pre">dtype</span></code></a>  attributes:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.type.html#numpy.dtype.type" title="numpy.dtype.type"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.type</span></code></a></p></td>
<td><p>The type object used to instantiate a scalar of this data-type.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.kind.html#numpy.dtype.kind" title="numpy.dtype.kind"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.kind</span></code></a></p></td>
<td><p>A character code (one of ‘biufcmMOSUV’) identifying the general kind of data.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.char.html#numpy.dtype.char" title="numpy.dtype.char"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.char</span></code></a></p></td>
<td><p>A unique character code for each of the 21 different built-in types.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.num.html#numpy.dtype.num" title="numpy.dtype.num"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.num</span></code></a></p></td>
<td><p>A unique number for each of the 21 different built-in types.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.str.html#numpy.dtype.str" title="numpy.dtype.str"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.str</span></code></a></p></td>
<td><p>The array-protocol typestring of this data-type object.</p></td>
</tr>
</tbody>
</table>
<p>Size of the data is in turn described by:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.name.html#numpy.dtype.name" title="numpy.dtype.name"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.name</span></code></a></p></td>
<td><p>A bit-width name for this data-type.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.itemsize.html#numpy.dtype.itemsize" title="numpy.dtype.itemsize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.itemsize</span></code></a></p></td>
<td><p>The element size of this data-type object.</p></td>
</tr>
</tbody>
</table>
<p>Endianness of this data:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.byteorder.html#numpy.dtype.byteorder" title="numpy.dtype.byteorder"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.byteorder</span></code></a></p></td>
<td><p>A character indicating the byte-order of this data-type object.</p></td>
</tr>
</tbody>
</table>
<p>Information about sub-data-types in a <a class="reference internal" href="../glossary.html#term-structured-data-type"><span class="xref std std-term">structured data type</span></a>:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.fields.html#numpy.dtype.fields" title="numpy.dtype.fields"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.fields</span></code></a></p></td>
<td><p>Dictionary of named fields defined for this data type, or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.names.html#numpy.dtype.names" title="numpy.dtype.names"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.names</span></code></a></p></td>
<td><p>Ordered list of field names, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there are no fields.</p></td>
</tr>
</tbody>
</table>
<p>For data types that describe sub-arrays:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.subdtype.html#numpy.dtype.subdtype" title="numpy.dtype.subdtype"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.subdtype</span></code></a></p></td>
<td><p>Tuple <code class="docutils literal notranslate"><span class="pre">(item_dtype,</span> <span class="pre">shape)</span></code> if this <a class="reference internal" href="generated/numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype</span></code></a> describes a sub-array, and None otherwise.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.shape.html#numpy.dtype.shape" title="numpy.dtype.shape"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.shape</span></code></a></p></td>
<td><p>Shape tuple of the sub-array if this data type describes a sub-array, and <code class="docutils literal notranslate"><span class="pre">()</span></code> otherwise.</p></td>
</tr>
</tbody>
</table>
<p>Attributes providing additional information:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.hasobject.html#numpy.dtype.hasobject" title="numpy.dtype.hasobject"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.hasobject</span></code></a></p></td>
<td><p>Boolean indicating whether this dtype contains any reference-counted objects in any fields or sub-dtypes.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.flags.html#numpy.dtype.flags" title="numpy.dtype.flags"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.flags</span></code></a></p></td>
<td><p>Bit-flags describing how this data type is to be interpreted.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.isbuiltin.html#numpy.dtype.isbuiltin" title="numpy.dtype.isbuiltin"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.isbuiltin</span></code></a></p></td>
<td><p>Integer indicating how this dtype relates to the built-in dtypes.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.isnative.html#numpy.dtype.isnative" title="numpy.dtype.isnative"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.isnative</span></code></a></p></td>
<td><p>Boolean indicating whether the byte order of this dtype is native to the platform.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.descr.html#numpy.dtype.descr" title="numpy.dtype.descr"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.descr</span></code></a></p></td>
<td><p><em class="xref py py-obj">__array_interface__</em> description of the data-type.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.alignment.html#numpy.dtype.alignment" title="numpy.dtype.alignment"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.alignment</span></code></a></p></td>
<td><p>The required alignment (bytes) of this data-type according to the compiler.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.base.html#numpy.dtype.base" title="numpy.dtype.base"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.base</span></code></a></p></td>
<td><p>Returns dtype for the base element of the subarrays, regardless of their dimension or shape.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="methods">
<h3>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h3>
<p>Data types have the following method for changing the byte order:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.newbyteorder.html#numpy.dtype.newbyteorder" title="numpy.dtype.newbyteorder"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.newbyteorder</span></code></a>([new_order])</p></td>
<td><p>Return a new dtype with a different byte order.</p></td>
</tr>
</tbody>
</table>
<p>The following methods implement the pickle protocol:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.dtype.__reduce__.html#numpy.dtype.__reduce__" title="numpy.dtype.__reduce__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.__reduce__</span></code></a>()</p></td>
<td><p>helper for pickle</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.dtype.__setstate__.html#numpy.dtype.__setstate__" title="numpy.dtype.__setstate__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dtype.__setstate__</span></code></a>()</p></td>
<td><p></p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>